
/**		
 * 
 *	uk.co.lmfm.guppy.view.ProjectMediator
 *	
 *	@version 1.00 | Jan 14, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view 
{
	import uk.co.lmfm.guppy.application.enum.ApplicationConstants;
	import uk.co.lmfm.guppy.model.vo.ClassLibraryVO;
	import uk.co.lmfm.guppy.model.vo.ConfigVO;
	import uk.co.lmfm.guppy.model.vo.FlashPlayerVO;
	import uk.co.lmfm.guppy.model.vo.ViewVO;
	import uk.co.lmfm.guppy.model.vo.WorkspaceVO;
	import uk.co.lmfm.guppy.view.components.events.ProjectViewEvent;
	import uk.co.lmfm.guppy.view.components.project.ProjectView;

	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	/**
	 * ProjectMediator
	 */
	public class ProjectMediator extends Mediator implements IMediator 
	{
		//	----------------------------------------------------------------
		//	CONSTANTS
		//	----------------------------------------------------------------

		public static const NAME : String = "ProjectMediator";

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function ProjectMediator(viewComponent : ProjectView)
		{
			super(NAME, viewComponent);
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		override public function listNotificationInterests() : Array
		{
			return [
					ApplicationConstants.POLL_VIEWS_FOR_DEFINITIONS,
					ApplicationConstants.GOT_CONFIG,
					ApplicationConstants.CHANGE_VIEW,
					ApplicationConstants.FOUND_WORKSPACE,
					ApplicationConstants.GET_WORKSPACES,
					ApplicationConstants.GOT_WORKSPACES
					];
		}

		override public function onRegister() : void
		{
			super.onRegister();
			
			component.addEventListener(ProjectViewEvent.CREATE_PROJECT, onCreateProject);
		}

		override public function handleNotification(notification : INotification) : void
		{
			super.handleNotification(notification);
			
			var name : String = notification.getName();
			var body : Object = notification.getBody();
			
			switch(name)
			{
				case ApplicationConstants.CHANGE_VIEW : 
				
					if(body == NAME)
					{
						sendNotification(ApplicationConstants.GET_CONFIG);
						sendNotification(ApplicationConstants.GET_WORKSPACES);
						component.show();
					}
					else
					{
						component.hide();
					}
				
					break;
					
				case ApplicationConstants.POLL_VIEWS_FOR_DEFINITIONS :
				
					var definition : ViewVO = new ViewVO();
			
					definition.name = ProjectMediator.NAME;
					definition.label = "Project Tools";
			
					sendNotification(ApplicationConstants.GOT_VIEW_DEFINITION, definition);
					
					break;
					
				case ApplicationConstants.FOUND_WORKSPACE :
					
					component.settingsPanel.addWorkspace(body as WorkspaceVO);
					
					break;
					
				case ApplicationConstants.GET_WORKSPACES :
					
					component.settingsPanel.clearWorkspaces();
					
					break;
				
				case ApplicationConstants.GOT_CONFIG :
				
					component.settingsPanel.clearPlayers();
					component.libraryPanel.clearClassLibraries();
					
					var config : ConfigVO = body as ConfigVO;
					
					for each (var player : FlashPlayerVO in config.playerVersions) 
					{
						component.settingsPanel.addPlayer(player);
					}
					
					for each (var lib : ClassLibraryVO in config.classLibraries) 
					{
						component.libraryPanel.addClassLibrary(lib);
					}
				
					break;
			}
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onCreateProject(event : ProjectViewEvent) : void
		{
			sendNotification(ApplicationConstants.CREATE_PROJECT, event.project);
		}

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get component() : ProjectView
		{
			return viewComponent as ProjectView;
		}
	}
}
